package sebjul.ecom.servlet;

import java.util.*;

import javax.ejb.*;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sebjul.ecom.dao.*;
import sebjul.ecom.entities.*;

@WebServlet(urlPatterns={"/NewArticle", "/EditArticle"})
public class NewArticle extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	@EJB
	GenreDao genreDao;
	
	@EJB
	ArticleDao articleDao;

    public NewArticle() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String articleIdStr = request.getParameter("articleId");
		if (articleIdStr!= null) {
			int articleId = Integer.parseInt(articleIdStr);
			Article article = articleDao.findArticleById(articleId);
			request.setAttribute("articleRegistration", article);
			request.setAttribute("editArticle", true);
		}
		
		List<Genre> genres = genreDao.findGenres();
		request.setAttribute("genres", genres);
		RequestDispatcher rd = request.getRequestDispatcher("addarticle.jsp");
		rd.forward(request, response);
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
